|
|
|
|
||
|
Tömb feltöltése véletlen számokkal és az elemek elrendezése buborék rendezéssel (Buborek.java) public class Buborek{ public static void main(String[]args){ int n,i,j,x; boolean vcs; int[] a=new int[50]; n=10; i=0; while (i<n){ a[i]=(int)((90*Math.random())+1); i=i+1; } System.out.println("A szamok rendezetlenul:"); i=0; while(i<n){ System.out.println(a[i]); i=i+1; } i=0; while (i<n){ j=n-1; vcs=false; while (j>i){ if(a[j]<a[j-1]){ x=a[j-1]; a[j-1]=a[j]; a[j]=x; vcs=true; } j=j-1; } if (vcs==false) break; i=i+1; } System.out.println("A szamok rendezetten:"); i=0; while(i<n){ System.out.println(a[i]); i=i+1; } } } ![]() Tömb feltöltése véletlen számokkal és az elemek elrendezése buborék rendezéssel, eljárások használatával (BuborekV1.java) public class BuborekV1{ public static int n=10; public static int[] a=new int[10]; public static void feltolt(){ int i; i=0; while (i<n){ a[i]=(int)((90*Math.random())+1); i=i+1; } } public static void kiir(){ int i; i=0; while(i<n) { System.out.println(a[i]); i=i+1; } } public static void cserel (int i,int j){ int x; x=a[j]; a[j]=a[i]; a[i]=x; } public static void rendez(){ int i,j; boolean vcs; i=0; while (i<n){ j=n-1; vcs=false; while(j>i){ if(szamok[j]<szamok[j-1]){ cserel(j,j-1); vcs=true; } j=j-1; } if(vcs==false) break; i=i+1; } } public static void main(String[] args){ feltolt(); kiir(); System.out.println(" "); rendez(); kiir(); } } |
||||